import {Component, HostBinding, DoCheck, ViewChild} from "@angular/core";
import {ActivatedRoute, NavigationEnd, PRIMARY_OUTLET, Router} from "@angular/router";
import {LocalStorage} from "./app.local-storage";
import {USER} from "./service/user.service";
import {LoginModalViewComponent} from "./views/login-modal-view/login-modal-view.component";
import {UserModel} from "./model/user.model";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements DoCheck{
  showLoginModal:boolean = false;
  title = 'app works!';
  @HostBinding('class') classes:any = [];
  city:any;

  constructor(private router:Router, private activated:ActivatedRoute,private ls:LocalStorage,private USER:USER) {
    this.router.events.subscribe((event) => {
      if (event instanceof NavigationEnd) {
        document.documentElement.scrollTop = document.body.scrollTop = 0;
      }
    });
    // 对路由中的数据进行一定处理
    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .map(_ => this.router.routerState)
      .map(state => state['firstChild'](this.activated))
      .filter(route => route.outlet === PRIMARY_OUTLET)
      .flatMap(route => route.data)
      .subscribe(data => {
        if (data['class']) {
          this.classes = data['class'];
        } else {
          this.classes = [];
        }
      });
    this.USER.goLogin.subscribe(v=> {
      console.log("USER.goLogin");
      this.showLoginModal = true;
    })
  }

  cityChanged(event:any){
    this.city=event;
    this.ls.setObject('city',this.city);
  }
  closeLogin(result:Object){
    if(result instanceof UserModel){
      this.USER.update(result);
    }
    this.showLoginModal=false;
  }

  ngDoCheck(): void {
  }
}
